package
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import crl.lottery.controller.LotteryController;
	import crl.lottery.model.LogicConfig;
	import crl.lottery.view.NoticeManager;
	
	import org.aswing.AsWingManager;
	
	/**
	 * 抽奖应用程序;
	 * @author crl;
	 * 
	 */	
	[SWF(width="500",height="500",frameRate="33")]
	public class Lottery extends Sprite
	{
		private var controller:LotteryController;
		private var config:LogicConfig;
		
		
		private var logic_cfg_url:String;
		private var lottery_result_url:String;
		
		
		public function Lottery()
		{
			this.addEventListener(Event.ADDED_TO_STAGE,addStageHandler);
		}
		private function addStageHandler(event:Event):void{
			stage.color=0;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			stage.quality=StageQuality.BEST;
			stage.addEventListener(Event.RESIZE,resizeHandler);
			
			AsWingManager.initAsStandard(this);
			
		
			var parameters:Object=this.loaderInfo.parameters;
			logic_cfg_url=parameters["cfg"] || "";
			
			
			LogicConfig.lottery_result_url=parameters["res"] || "";
			LogicConfig.lottery_start_url=parameters["start"] || "";
			
			var skin:String=parameters["skin"];
			
			if(!skin){
				skin="assets/cj.swf";
			}
			
			var skinLoader:Loader=new Loader();
			skinLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,init);
			skinLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandle);
			skinLoader.load(new URLRequest(skin),new LoaderContext(false,ApplicationDomain.currentDomain));	
		}
		
		protected function errorHandle(event:Event):void
		{
			// TODO Auto-generated method stub
			trace("加载皮肤失败");
		}		
		
		private function init(event:Event):void{
			controller=new LotteryController();
			
			var view:DisplayObject=controller.view;
			this.addChild(view);
			
			config=new LogicConfig();
			config.addEventListener(Event.COMPLETE,configCompleteHandler);
			config.addEventListener(ErrorEvent.ERROR,configErrorHandler);
			if(logic_cfg_url==""){
				logic_cfg_url="data/Ernie.xml";
			}
			config.load(logic_cfg_url);
			
			resizeHandler(event);
		}
		
		
		
		private function configCompleteHandler(event:Event):void{
			controller.config=config;
		}
		private function configErrorHandler(event:ErrorEvent):void{
			NoticeManager.alert("加载配置出错",event.text+"\n请刷新浏览器重试",null,0);
			
			this.controller.view.mouseChildren=false;
		}
		
		
		private function resizeHandler(event:Event):void{
			controller.view.x=(stage.stageWidth>>1);
			controller.view.y=(stage.stageHeight>>1);
		}
	}
}